/**
 * 版权所有 2009-2012山东新北洋信息技术股份有限公司
 * 保留所有权利。
 */
package com.linyaonan.leetcode.medium._1379;

/**
 * @ProjectName: leetcode
 * @Package: com.linyaonan.leetcode.medium._1379
 * @ClassName: GetTargetCopy
 * @Author: linyaonan
 * @Date: 2020/3/24 11:20
 */
public class GetTargetCopy {
    public static TreeNode get = null;
    public final TreeNode getTargetCopy(final TreeNode original, final TreeNode cloned, final TreeNode target) {
        TreeNode t;
        try {
            t = dfs(original, target, cloned);
        } catch (Exception e) {
            t = get;
        }
        return t;
    }

    private TreeNode dfs(TreeNode node, TreeNode t, TreeNode s) throws Exception {
        if (node != null) {
            if (node.val == t.val) {
                get = s;
                // 找到后直接抛出异常进行剪枝
                throw new RuntimeException("get it");
            } else {
                dfs(node.left, t, s.left);
                dfs(node.right, t, s.right);
            }
        }
        return null;
    }

}
